home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyAppleEvents.p < prev    next >
Encoding:
Text File  |  1994-08-29  |  4.3 KB  |  144 lines  |  [TEXT/PJMM]

  1. unit MyAppleEvents;
  2.  
  3. interface
  4.  
  5. {$IFC undefined THINK_Pascal}
  6.     uses
  7.         Types;
  8. {$ENDC}
  9.  
  10.     const
  11.         keyOdocOption = 'auto'; { optional parameter to open }
  12.  
  13.     var
  14.         odoc_with_option: boolean;
  15.  
  16.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  17. { function DoOApp: OSErr }
  18. { function DoODoc (fs: FSSpec): OSErr }
  19. { function DoPrint (fs: FSSpec): OSErr }
  20. { function DoQuit: OSErr}
  21.  
  22. implementation
  23.  
  24.     uses
  25. {$IFC undefined THINK_Pascal}
  26.         OSEvents, 
  27. {$ENDC}
  28.         EPPC, Notification, AppleEvents, MyAEUtils;
  29.  
  30.     function DoOApp (p: ptr): OSErr;
  31.     inline
  32.         $205F, $4E90;
  33.  
  34.     function DoDocs (fs: FSSpec; p: ptr): OSErr;
  35.     inline
  36.         $205F, $4E90;
  37.  
  38.     function DoQuit (p: ptr): OSErr;
  39.     inline
  40.         $205F, $4E90;
  41.  
  42.     const
  43.         kPatienceLevel = 1000;                                { <aevt> ticks we wait for response }
  44.         kSysEnvironsVersion = 1;
  45.         kOSEvent = app4Evt;    {event used by MultiFinder}
  46.         kSuspendResumeMessage = 1;        {high byte of suspend/resume event message}
  47.         kResumeMask = 1;        {bit of message field for resume vs. suspend}
  48.         kMouseMovedMessage = $FA;        {high byte of mouse-moved event message}
  49.         kNoEvents = 0;        {no events mask}
  50.         kNoListChosen = -1;
  51.  
  52.     function HandleOAPP (event, reply: AppleEvent; openappp: ptr): OSErr;{ <aevt> }
  53.         var
  54.             oe: OSErr;
  55.     begin
  56.     { We don't expect any params at all, but check in case the client requires any }
  57.         oe := GotRequiredParams(event);
  58.         oe := DoOApp(openappp);
  59.         HandleOAPP := oe;
  60.     end;
  61.  
  62.     function HandleDocs (event, reply: AppleEvent; dodocp: ptr): OSErr;        { <aevt> }
  63.         var
  64.             myFSS: FSSpec;
  65.             docList: AEDescList;
  66.             index, itemsInList: LONGINT;
  67.             actualSize: Size;
  68.             keywd: AEKeyword;
  69.             typeCode: descType;
  70.             oe, ooe, err: OSErr;
  71.             er: EventRecord;
  72.             realType: DescType;
  73.             realSize: longInt;
  74.             psn: ProcessSerialNumber;
  75.             pi: ProcessInfoRec;
  76.     begin
  77.         err := GetBooleanFromAERecord(event, keyOdocOption, odoc_with_option);
  78.         if err <> noErr then begin
  79.             odoc_with_option := false;
  80.             err := AEGetAttributePtr(event, keyAddressAttr, keyProcessSerialNumber, realType, @psn, SizeOf(psn), realSize);
  81.             if err = noErr then begin
  82.                 pi.processInfoLength := sizeof(ProcessInfoRec);
  83.                 pi.processName := nil;
  84.                 pi.processAppSpec := nil;
  85.                 err := GetProcessInformation(psn, pi);
  86.             end;
  87.             if (err = noErr) & (pi.processSignature = 'MACS') then begin
  88.                 odoc_with_option := OSEventAvail(everyEvent, er);
  89.                 odoc_with_option := BAND(er.modifiers, optionKey) <> 0;
  90.             end;
  91.         end;
  92.         oe := AEGetParamDesc(event, keyDirectObject, typeAEList, docList);
  93.         if oe = noErr then begin
  94.             ooe := GotRequiredParams(event);
  95.     { now get each alias from the list (as an FSSSpec) and open the associated file. }
  96.             oe := AECountItems(docList, itemsInList);
  97.             for index := 1 to itemsInList do begin
  98.                 ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  99. { coercion does alias->fsspec }
  100.                 if ooe = noErr then
  101.                     ooe := DoDocs(myFSS, dodocp);
  102.             end;
  103.             ooe := AEDisposeDesc(docList);
  104.         end;
  105.         HandleDocs := oe;
  106.     end; { HandleDocs }
  107.  
  108.     function HandleQUIT (event, reply: AppleEvent; quitp: ptr): OSErr;        { <aevt> }
  109.         var
  110.             oe: OSErr;
  111.             errStr: Str255;
  112.     begin
  113.     { We don't expect any params at all, but check in case the client requires any }
  114.         oe := GotRequiredParams(event);
  115.         oe := DoQuit(quitp);            { set global boolean: app will exit at end of event loop }
  116.         if reply.dataHandle <> nil then            { a reply is sought }
  117.             begin
  118.             if oe = noErr then
  119.                 errStr := 'OK'
  120.             else
  121.                 errStr := 'user cancelled quit';
  122.             oe := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  123.         end;
  124.         HandleQUIT := oe;
  125.     end;
  126.  
  127. {$S Init}
  128.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  129.         var
  130.             aevtErr: OSErr;
  131.     begin
  132.         aevtErr := noErr;
  133.         if (aevtErr = noErr) and (DoOApp <> nil) then
  134.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longInt(DoOApp), false);
  135.         if (aevtErr = noErr) and (DoODoc <> nil) then
  136.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longInt(DoODoc), false);
  137.         if (aevtErr = noErr) and (DoPrint <> nil) then
  138.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longInt(DoPrint), false);
  139.         if (aevtErr = noErr) and (DoQuit <> nil) then
  140.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longInt(DoQuit), false);
  141.         InitAppleEvents := aevtErr;
  142.     end;
  143.  
  144. end.